package leetcode.math;

public class Lc29 {


    public int divide(int dividend, int divisor) {

        if (divisor == dividend) return 1;
        if (divisor == 1) return dividend;
        if (divisor == -1 && dividend != Integer.MIN_VALUE) return -dividend;
        int flag = 1;
        if (dividend < 0 && divisor > 0 || dividend > 0 && divisor < 0)
            flag = -1;
        int ans = 0;
        if (dividend == Integer.MIN_VALUE ) {
            if (divisor == -1) return Integer.MAX_VALUE;

            if (divisor < 0) {
                dividend -= divisor;
                ans++;
            } else {
                dividend += divisor;
                ans++;
            }
        } else if (divisor == Integer.MIN_VALUE) return 0;

        dividend = Math.abs(dividend);
        divisor = Math.abs(divisor);

        while (dividend >= divisor) {
            dividend -= divisor;
            ans++;
        }
        return ans * flag;
    }

    public static void main(String[] args) {
        Lc29 lc29 = new Lc29();
        System.out.println(lc29.divide(-2147483648, -1));
    }

}
